#include"linked_list.h"

SLTNode* removeElements(SLTNode* head, int val) {

    SLTNode* pcur1 = head;
    SLTNode* pcur2 = head;
    SLTNode* phead = NULL;
    SLTNode* tail = phead;
    while (pcur1)
    {
        pcur1 = pcur2->next;
        if (pcur2->data == val)
        {
            free(pcur2);
        }
        else
        {
            if (phead == NULL)
            {
                tail = phead = pcur2;
            }
            else
            {
                tail->next = pcur2;
                tail = tail->next;
            }
        }
        pcur2 = pcur1;
    }

    if (phead == NULL)
    {
        return phead;
    }
    else
    {
        tail->next = NULL;
        return phead;
    }
}

void test()
{
    SLTNode* phead = NULL;
    SLTPushBack(&phead, 1);
    SLTPushBack(&phead, 2);
    SLTPushBack(&phead, 3);
    SLTPushBack(&phead, 4);
    SLTPushBack(&phead, 5);
    SLTPushBack(&phead, 6);
    SLTPushBack(&phead, 7);
    SLTPrint(phead);
    /*SLTPushBack(&phead, 5);
    SLTPushBack(&phead, 5);
    SLTPushBack(&phead, 5);
    SLTPushBack(&phead, 5);
    SLTPrint(phead);*/
    phead = removeElements(phead, 7);
    SLTPrint(phead);
    SListDesTroy(&phead);

}

//struct ListNode* middleNode(struct ListNode* head) {
//    if (head == NULL)
//        return NULL;
//    struct ListNode* fast, slow;
//    fast = slow = head;
//    if (fast->next == NULL)
//        return fast;
//    while (1)
//    {
//        slow = slow->next;
//        fast = fast->next;
//        if (fast->next == NULL)
//        {
//            return slow;
//        }
//        ast = fast->next;
//        if (fast->next == NULL)
//        {
//            return slow;
//        }
//    }
//}

int main()
{
    test();
    return 0;
}